#!/bin/bash -e
# install vm kernel

get_os_release() {
    local os_release result
    os_release=`grep -i '^name=' /etc/os-release | awk -F \" '{print $2}' | tr "[:upper:]" "[:lower:]"`
    if [ "X${os_release:0:6}" = "Xubuntu" ]; then
        result="ubuntu"
    elif [ "X${os_release:0:6}" = "Xcentos" ]; then
        result="centos"
    elif [ "X${os_release:0:6}" = "Xanolis" ]; then
        result="anolis"
    elif [ "X${os_release:0:5}" = "Xkylin" ]; then
        result="kylin"
    else
        echo "Unknown OS release"
        exit 1
    fi
    echo $result
}

os_release=$(eval get_os_release)
case $os_release in
    "anolis"|"kylin")
        yum install -y cmake autoconf gcc make rpm-build gcc-c++ automake git libdrm yum-utils
        yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        yum install -y docker-ce docker-ce-cli containerd.io
        systemctl enable docker
        systemctl restart docker
        lsblk
        growpart /dev/sda 2
        resize2fs /dev/sda2
        tar xvf kernel.tgz
        rpm -ivh --force --nodeps kernel/*.rpm
    ;;
    "ubuntu")
        mkdir -p /etc/apt/keyrings
        curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
        echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

        apt-get update
        apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
        lsblk
        growpart /dev/sda 1
        resize2fs /dev/sda1
        apt-get update
        apt-get install -y gcc make cmake flex bison build-essential libelf-dev libssl-dev bc rsync kmod cpio openssl python python3 ninja-build pkg-config libglib2.0-dev git meson libpixman-1-dev libcap-ng-dev automake gettext autopoint grub2-common dosfstools mtools nasm iasl mlocate
        tar xvf kernel.tgz
        dpkg -i kernel/*deb
    ;;
esac

rm -rf  kernel.tgz  kernel
